package com.pisces.tool.query;


import java.util.*;

public class QueryBuilder {

    private List<Object> params;
    private LinkedHashMap<String, Integer> sort;
    private Integer limit;

    private int pageNumber = 1;	//页码
    private int pageSize = 20;	//每页几条

    private StringBuilder queryString;

    public QueryBuilder() {
        this.params = new ArrayList<Object>();
        this.sort = new LinkedHashMap<String, Integer>();
        this.queryString = new StringBuilder(128);
    }

    public QueryBuilder(int pageNumber, int pageSize) {
        this.params = new ArrayList<Object>();
        this.sort = new LinkedHashMap<String, Integer>();
        this.queryString = new StringBuilder(128);

        this.pageNumber = pageNumber;
        this.pageSize = pageSize;
    }

    /**
     * 传入组织好的sql文,可以带参数?
     * @param scope
     * @param paras
     * @return
     */
    public QueryBuilder addQuery(String scope, Object... paras) {
		if (scope != null) {
			queryString.append(" AND " + scope);
			if (paras != null && paras.length > 0) {
				for (int i = 0; i < paras.length; i++)
					params.add(paras[i]);
			}
		}
		return this;
	}

    public QueryBuilder addSort(String name, int sortType) {
        this.sort.put(name, sortType);
        return this;
    }

    public QueryBuilder addSort(LinkedHashMap<String, Integer> sort) {
        this.sort.putAll(sort);
        return this;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    /**
     * 获取查询子句，形如：
     * 1 = 1 and name = 'xx' ORDER BY `id` ASC, `age` DESC
     *
     * @return
     */
    public String getQueryString() {
        return queryString.append(getSortString()).append(getLimitString()).toString();
    }

    public List<Object> getParams() {
        return params;
    }

    /**
     * 获取排序子句，形如：
     * ORDER BY `id` ASC, `age` DESC
     *
     * @return String
     */
    private String getSortString() {
        Set<String> columns = sort.keySet();
        if (columns.size() == 0) {
            return "";
        }
        StringBuilder sortString = new StringBuilder(" ORDER BY");
        Iterator<String> names = columns.iterator();
        while (names.hasNext()) {
            String name = names.next();
            sortString.append(" `").append(name).append("` ");
            if (sort.get(name) == QueryObject.SORT_ASC) {
                sortString.append("ASC,");
            } else {
                sortString.append("DESC,");
            }
        }
        return sortString.substring(0, sortString.length() - 1);
    }

    private String getLimitString() {
        String limitString = "";
        if (limit != null) {
            limitString += " LIMIT " + limit;
        }
        return limitString;
    }

    public int getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
}
